﻿using SQ.BLL.Factory;
using SQ.BLL.Implementation;
using SQ.BLL.Interfaces;
using SQ.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SQ.API.Controllers
{
    [RoutePrefix("api/usuario")]
    public class UsuarioController : ApiController
    {
        private IUsuarioBS userBS = BLLFactory.getUsuarioBS();

        public HttpResponseMessage Get()
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, this.userBS.ListarUsuarios());
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

        }

        [Route("{email}")]
        [HttpGet]
        public HttpResponseMessage Get(string email)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, this.userBS.ListarUsuarios(email));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

        }

        [HttpPost]
        public HttpResponseMessage Post(Usuario usuario)
        {
            if (usuario == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            try
            {
                this.userBS.CriarUsuario(usuario);
                return Request.CreateResponse(HttpStatusCode.OK, usuario);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [HttpPut]
        public HttpResponseMessage Put(Usuario usuario)
        {
            if (usuario == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            try
            {
                this.userBS.AlterarUsuario(usuario);
                return Request.CreateResponse(HttpStatusCode.OK, usuario);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("{usuario}")]
        [HttpDelete]
        public HttpResponseMessage Delete(int usuario)
        {
            if (usuario == 0)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            try
            {
                this.userBS.ExcluirUsuario(usuario);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
    }
}